package com.zzptc.zzsgz.bdsjws.services;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.Activity_Update;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;

import java.io.File;

import okhttp3.Call;
import okhttp3.OkHttpClient;

import static android.content.ContentValues.TAG;

public class MyDownLoadService extends Service {

    private static NotificationManager notificationManager;

    private static NotificationCompat.Builder  notificationBuild;

    public MyDownLoadService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String url=intent.getStringExtra("apkUrl");
//        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
        downLoad(url,MyUtil.getPath());

        return super.onStartCommand(intent, flags, startId);
    }

    public void downLoad(String url, final File file){
        createNotification();
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(file.getAbsolutePath(),"bd.apk") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                       notificationBuild.setContentText("下载失败");
                        notificationBuild.setContentTitle("下载失败");
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        Intent intent=MyUtil.getInstallIntent(response);
                        TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyDownLoadService.this);
                        stackBuilder.addParentStack(Activity_Update.class);
                        stackBuilder.addNextIntent(intent);
                        PendingIntent resultPendingIntent =
                                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                        notificationBuild.setContentIntent(resultPendingIntent);
                        //让进度条消失
                        notificationBuild.setProgress(0,0,false);
                        notificationBuild.setContentText("请点击安装");
                        notificationBuild.setContentTitle("下载完毕");
                        notificationManager.notify(0,notificationBuild.build());


                    }

                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        Log.e(TAG,progress+"" );
                        int i= (int) (progress*100);
//                        if(i%10==0){
                            notificationBuild.setProgress(100,i,false);
                            notificationBuild.setContentText("已经下载了"+i+"%");
                            notificationManager.notify(id,notificationBuild.build());
//                        }
                    }
                });
    }

    public static void createNotification(){
         notificationManager= (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuild = new NotificationCompat.Builder(MyApplication.getContext());
        notificationBuild.setContentTitle("下载中……")
                .setSmallIcon(R.mipmap.logo_small)
                .setAutoCancel(true);
        notificationManager.notify(0, notificationBuild.build());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
